package com.nuoniu.sibanyun.service.erp.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.entity.erp.ErpProject;
import com.nuoniu.sibanyun.entity.erp.ErpProjectPlan;
import com.nuoniu.sibanyun.mapper.erp.ErpProjectPlanMapper;
import com.nuoniu.sibanyun.service.erp.IErpProjectPlanService;
import com.nuoniu.sibanyun.service.erp.IErpProjectService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 项目计划 服务实现类
 * </p>
 *
 * @author 小啊刚
 * @since 2021-10-24
 */
@Service
public class ErpProjectPlanServiceImpl extends ServiceImpl<ErpProjectPlanMapper, ErpProjectPlan> implements IErpProjectPlanService {

    @Autowired
    private IErpProjectService projectService;

    @Override
    public List<ErpProjectPlan> getList(ErpProjectPlan projectPlan) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("project_id", projectPlan.getProjectId());
        if(projectPlan.getType() != null) {
            wrapper.eq("type", projectPlan.getType());
        }

        wrapper.orderByAsc("end_date");
        return list(wrapper);
    }

    @Override
    public void savePlan(ErpProjectPlan projectPlan) {
        UserInfo userInfo = UserThreadLocal.get();
        if(projectPlan.getParentId() != null) {
            ErpProjectPlan parentPlan = this.getById(projectPlan.getParentId());
            if(parentPlan != null) {
                parentPlan.setTreeLeaf(false);
                parentPlan.setParentIds(parentPlan.getParentIds() + "," + parentPlan.getId());
                projectPlan.setTreeLevel(parentPlan.getTreeLevel() + 1);
                projectPlan.setUpdateDate(LocalDateTime.now());
                updateById(parentPlan);
            }
        }

        if(projectPlan.getId() == null) {
            projectPlan.setCreateUserId(userInfo.getUserId());
            projectPlan.setCreateUserName(userInfo.getRealName());
        } else {
            projectPlan.setUpdateUserId(userInfo.getUserId());
            projectPlan.setUpdateUserName(userInfo.getRealName());
        }

        saveOrUpdate(projectPlan);

        ErpProject project = projectService.getById(projectPlan.getProjectId());
        project.setWorkNumber(project.getWorkNumber() + 1);
        project.setNotWorkNumber(project.getNotWorkNumber() + 1);
        projectService.updateById(project);
    }

    @Override
    public void delete(Long id) {
        Integer count = baseMapper.getCountByParentId(id);
        if(count > 0) {
            throw new NuoNiuException("请先删除子级");
        }

        removeById(id);

        ErpProjectPlan projectPlan = getById(id);
//        QueryWrapper wrapper = new QueryWrapper();
//        wrapper.in("parent_ids", id);
//        remove(wrapper);

        ErpProject project = projectService.getById(projectPlan.getProjectId());
        project.setWorkNumber(project.getWorkNumber() - 1);
        projectService.updateById(project);
    }


}
